package first.endtoend

import first.endtoend.Family
import first.endtoend.PortfolioDetail

/*
this service is related to updating portfolio after retrieving an aid
 */

class RetrieveAidService
{


    def successRetrieveAid(Family aFamily, def details)
    {

        if (!aFamily)
            return false
        Set<PortfolioDetail> list = aFamily.portfolio.portfolioDetails


        if (list != null)
        {
            list.each { detail ->
                details.each { d ->
                    println("here quatity recovered " + d.quantityRecovered)
                    //detail.quantity = detail.id == d.id  ? detail.quantity - d.quantityRecovered : detail.quantity
                    if (detail.product.id == d.product.id)
                    {
                        println("product is the same")
                        if (detail.quantity - d.quantityRecovered < 0)
                        {
                            println("could not retrieve this qte!!")
                            return false
                        } else
                        {
                            println("executing update quantity")
                            detail.quantity = detail.quantity - d.quantityRecovered
                            detail.save(flush: true)
                            if (detail.hasErrors())
                                detail.errors.each {
                                    println it
                                }
                        }
                    } else
                    {
                        //not the correct detail
                    }
                }
            }
        }
        return true
    }


}
